import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:speed_random/common/common_fuc.dart';
import 'package:speed_random/core/http/api_app_info.dart';
import 'package:speed_random/core/http/api_user_info.dart';
import 'package:speed_random/global_data.dart';
import 'package:speed_random/language18n.dart';
import 'package:speed_random/pages/home/index.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:wakelock_plus/wakelock_plus.dart';
import 'router.dart';
import 'package:get/get.dart';





void myMain() async {

  WidgetsFlutterBinding.ensureInitialized();
  globalData.userId=await commonFuc.generateDeviceId();


ApiAppInfo.get();
ApiUserInfo.get();

//globalData.userInfo.isvip=true;

  // 启用屏幕常亮功能
  WakelockPlus.enable();



  // 预加载设置
  final prefs = await SharedPreferences.getInstance();
  globalData.soundPath = prefs.getString('soundPath') ?? globalData.soundPath;
  globalData.soundSuccessPath = prefs.getString('soundSuccessPath') ?? globalData.soundSuccessPath;
  globalData.soundPathName = prefs.getString('soundPathName') ?? globalData.soundPathName;
  globalData.soundSuccessPathName = prefs.getString('soundSuccessPathName') ?? globalData.soundSuccessPathName;

  globalData.bgColor=commonFuc.hexToColor(prefs.getString('app_theme_bg') ?? "#2196F3");



  // 获取系统语言
  final systemLocale = WidgetsBinding.instance.window.locale;
  final savedLanguage = prefs.getString('app_language');
  globalData.language = savedLanguage ?? systemLocale.languageCode;
  // 获取应用主题
  globalData.appThemeIndex = prefs.getInt('app_theme') ?? 0;

}

class MyApp extends StatelessWidget {

  const MyApp({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'Speed Random',
      debugShowCheckedModeBanner: false,
      translations: Language18n(),
      locale: Locale(globalData.language)??Locale('en', 'US'),// Locale('zh', 'CN'),
      fallbackLocale: Locale('en', 'US'),
      getPages: GetMyRouters,
      builder: EasyLoading.init(),

        /*
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),*/
      darkTheme: ThemeData.dark(),
      themeMode: ThemeMode.values[globalData.appThemeIndex],
      home: IndexPage(),
    );
  }
}



